package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.LeaseListBean;
import com.csweimei.rentmanagementtreasure.pop.PopAttach;
import com.csweimei.rentmanagementtreasure.pop.PopBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import retrofit2.http.PUT;

public class MLeaseVM extends TitleVM {
    public ObservableField<String> keyword = new ObservableField<>();
    public ObservableField<String> state = new ObservableField<>();
    public ObservableField<String> housingType = new ObservableField<>();
    public ObservableField<String> paixu = new ObservableField<>();

    public MLeaseVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("租约管理");
    }

    @Override
    public void onResume() {
        super.onResume();
        getLeaseList();

    }

    LinearLayout view;
    public void setView(LinearLayout llRoot) {
        view = llRoot;
    }

    public BindingCommand htStatus = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            List<PopBean>   list = new ArrayList<>();
            list.add(new PopBean("全部"));
            list.add(new PopBean("签约中"));
            list.add(new PopBean("已过期"));
            PopAttach popAttach = new PopAttach(mActivity, list, new PopAttach.Click() {
                @Override
                public void click(PopBean s) {
                    switch (s.getName()){
                        case "全部":
                            state.set("");
                            break;
                        case "签约中":
                            state.set("0");
                            break;
                        case "已过期":
                            state.set("1");
                            break;
                    }

                    getLeaseList();
                }
            });

            PopUtils.showAttachPop(mActivity,view,popAttach);
        }
    });

    public BindingCommand houseType = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            List<PopBean>   list = new ArrayList<>();
            list.add(new PopBean("全部"));
            list.add(new PopBean("整租"));
            list.add(new PopBean("合租"));
            PopAttach popAttach = new PopAttach(mActivity, list, new PopAttach.Click() {
                @Override
                public void click(PopBean s) {
                    housingType.set(s.getName());
                    getLeaseList();
                }
            });

            PopUtils.showAttachPop(mActivity,view,popAttach);
        }
    });

    public BindingCommand htPx = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            List<PopBean>   list = new ArrayList<>();
            list.add(new PopBean("默认"));
            list.add(new PopBean("按起租日从近到远"));
            list.add(new PopBean("按到期日从近到远"));
            PopAttach popAttach = new PopAttach(mActivity, list, new PopAttach.Click() {
                @Override
                public void click(PopBean s) {
                    switch (s.getName()){
                        case "默认":
                            paixu.set("0");
                            break;
                        case "按起租日从近到远":
                            paixu.set("1");
                            break;
                        case "按到期日从近到远":
                            paixu.set("2");
                            break;
                    }
                    getLeaseList();
                }
            });

            PopUtils.showAttachPop(mActivity,view,popAttach);
        }
    });


    public ObservableList<LeaseListItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<LeaseListItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_lease);

    public ObservableField<Integer> showNo = new ObservableField<>(View.GONE);
    public void getLeaseList() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("keyword",keyword.get());
        map.put("state",state.get());
        map.put("housingType",housingType.get());
        map.put("paixu",paixu.get());
        model.leaselist(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<LeaseListBean>() {
                    @Override
                    public void onNext(LeaseListBean o) {
                        observableList.clear();
                        if (o.issucc){
                            if (o.getData().size() > 0){
                                showNo.set(View.GONE);
                                for (LeaseListBean.DataDTO dataDTO:o.getData()) {
                                    LeaseListItemVM leaseListItemVM = new LeaseListItemVM(MLeaseVM.this);
                                    leaseListItemVM.setData(dataDTO);
                                    observableList.add(leaseListItemVM);

                                }
                            }
                        }else {
                            showNo.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
